{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 前言"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. `owner = np.random.randint(15000, 73000, (398,))`\n",
    "2. `pd.concat(df1, df2), df1.append(df2)` 上下合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MPG Cars"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction:\n",
    "\n",
    "The following exercise utilizes data from [UC Irvine Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/Auto+MPG)\n",
    "\n",
    "### Step 1. Import the necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2. Import the first dataset [cars1](https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/05_Merge/Auto_MPG/cars1.csv) and [cars2](https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/05_Merge/Auto_MPG/cars2.csv).  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   ### Step 3. Assign each to a variable called cars1 and cars2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "url1 = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/05_Merge/Auto_MPG/cars1.csv'\n",
    "url2 = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/05_Merge/Auto_MPG/cars2.csv'\n",
    "cars1 = pd.read_csv(url1)\n",
    "cars2 = pd.read_csv(url2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4. Oops, it seems our first dataset has some unnamed blank columns, fix cars1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model</th>\n",
       "      <th>origin</th>\n",
       "      <th>car</th>\n",
       "      <th>Unnamed: 9</th>\n",
       "      <th>Unnamed: 10</th>\n",
       "      <th>Unnamed: 11</th>\n",
       "      <th>Unnamed: 12</th>\n",
       "      <th>Unnamed: 13</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307</td>\n",
       "      <td>130</td>\n",
       "      <td>3504</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350</td>\n",
       "      <td>165</td>\n",
       "      <td>3693</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318</td>\n",
       "      <td>150</td>\n",
       "      <td>3436</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304</td>\n",
       "      <td>150</td>\n",
       "      <td>3433</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>amc rebel sst</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302</td>\n",
       "      <td>140</td>\n",
       "      <td>3449</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>ford torino</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement horsepower  weight  acceleration  model  \\\n",
       "0  18.0          8           307        130    3504          12.0     70   \n",
       "1  15.0          8           350        165    3693          11.5     70   \n",
       "2  18.0          8           318        150    3436          11.0     70   \n",
       "3  16.0          8           304        150    3433          12.0     70   \n",
       "4  17.0          8           302        140    3449          10.5     70   \n",
       "\n",
       "   origin                        car  Unnamed: 9  Unnamed: 10  Unnamed: 11  \\\n",
       "0       1  chevrolet chevelle malibu         NaN          NaN          NaN   \n",
       "1       1          buick skylark 320         NaN          NaN          NaN   \n",
       "2       1         plymouth satellite         NaN          NaN          NaN   \n",
       "3       1              amc rebel sst         NaN          NaN          NaN   \n",
       "4       1                ford torino         NaN          NaN          NaN   \n",
       "\n",
       "   Unnamed: 12  Unnamed: 13  \n",
       "0          NaN          NaN  \n",
       "1          NaN          NaN  \n",
       "2          NaN          NaN  \n",
       "3          NaN          NaN  \n",
       "4          NaN          NaN  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars1.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5. What is the number of observations in each dataset?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model</th>\n",
       "      <th>origin</th>\n",
       "      <th>car</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307</td>\n",
       "      <td>130</td>\n",
       "      <td>3504</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350</td>\n",
       "      <td>165</td>\n",
       "      <td>3693</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318</td>\n",
       "      <td>150</td>\n",
       "      <td>3436</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304</td>\n",
       "      <td>150</td>\n",
       "      <td>3433</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>amc rebel sst</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302</td>\n",
       "      <td>140</td>\n",
       "      <td>3449</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>ford torino</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement horsepower  weight  acceleration  model  \\\n",
       "0  18.0          8           307        130    3504          12.0     70   \n",
       "1  15.0          8           350        165    3693          11.5     70   \n",
       "2  18.0          8           318        150    3436          11.0     70   \n",
       "3  16.0          8           304        150    3433          12.0     70   \n",
       "4  17.0          8           302        140    3449          10.5     70   \n",
       "\n",
       "   origin                        car  \n",
       "0       1  chevrolet chevelle malibu  \n",
       "1       1          buick skylark 320  \n",
       "2       1         plymouth satellite  \n",
       "3       1              amc rebel sst  \n",
       "4       1                ford torino  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars1 = cars1.loc[:,'mpg':'car']\n",
    "cars1.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6. Join cars1 and cars2 into a single DataFrame called cars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model</th>\n",
       "      <th>origin</th>\n",
       "      <th>car</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33.0</td>\n",
       "      <td>4</td>\n",
       "      <td>91</td>\n",
       "      <td>53</td>\n",
       "      <td>1795</td>\n",
       "      <td>17.4</td>\n",
       "      <td>76</td>\n",
       "      <td>3</td>\n",
       "      <td>honda civic</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>6</td>\n",
       "      <td>225</td>\n",
       "      <td>100</td>\n",
       "      <td>3651</td>\n",
       "      <td>17.7</td>\n",
       "      <td>76</td>\n",
       "      <td>1</td>\n",
       "      <td>dodge aspen se</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>6</td>\n",
       "      <td>250</td>\n",
       "      <td>78</td>\n",
       "      <td>3574</td>\n",
       "      <td>21.0</td>\n",
       "      <td>76</td>\n",
       "      <td>1</td>\n",
       "      <td>ford granada ghia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>18.5</td>\n",
       "      <td>6</td>\n",
       "      <td>250</td>\n",
       "      <td>110</td>\n",
       "      <td>3645</td>\n",
       "      <td>16.2</td>\n",
       "      <td>76</td>\n",
       "      <td>1</td>\n",
       "      <td>pontiac ventura sj</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.5</td>\n",
       "      <td>6</td>\n",
       "      <td>258</td>\n",
       "      <td>95</td>\n",
       "      <td>3193</td>\n",
       "      <td>17.8</td>\n",
       "      <td>76</td>\n",
       "      <td>1</td>\n",
       "      <td>amc pacer d/l</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement horsepower  weight  acceleration  model  \\\n",
       "0  33.0          4            91         53    1795          17.4     76   \n",
       "1  20.0          6           225        100    3651          17.7     76   \n",
       "2  18.0          6           250         78    3574          21.0     76   \n",
       "3  18.5          6           250        110    3645          16.2     76   \n",
       "4  17.5          6           258         95    3193          17.8     76   \n",
       "\n",
       "   origin                 car  \n",
       "0       3         honda civic  \n",
       "1       1      dodge aspen se  \n",
       "2       1   ford granada ghia  \n",
       "3       1  pontiac ventura sj  \n",
       "4       1       amc pacer d/l  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cars2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model</th>\n",
       "      <th>origin</th>\n",
       "      <th>car</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307</td>\n",
       "      <td>130</td>\n",
       "      <td>3504</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350</td>\n",
       "      <td>165</td>\n",
       "      <td>3693</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318</td>\n",
       "      <td>150</td>\n",
       "      <td>3436</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304</td>\n",
       "      <td>150</td>\n",
       "      <td>3433</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>amc rebel sst</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302</td>\n",
       "      <td>140</td>\n",
       "      <td>3449</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>ford torino</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement horsepower  weight  acceleration  model  \\\n",
       "0  18.0          8           307        130    3504          12.0     70   \n",
       "1  15.0          8           350        165    3693          11.5     70   \n",
       "2  18.0          8           318        150    3436          11.0     70   \n",
       "3  16.0          8           304        150    3433          12.0     70   \n",
       "4  17.0          8           302        140    3449          10.5     70   \n",
       "\n",
       "   origin                        car  \n",
       "0       1  chevrolet chevelle malibu  \n",
       "1       1          buick skylark 320  \n",
       "2       1         plymouth satellite  \n",
       "3       1              amc rebel sst  \n",
       "4       1                ford torino  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car = pd.concat([cars1, cars2])\n",
    "car.head()\n",
    "\n",
    "# 还有append可以使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7. Oops, there is a column missing, called owners. Create a random number Series from 15,000 to 73,000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function shuffle in module random:\n",
      "\n",
      "shuffle(self, x, random=None)\n",
      "    Shuffle list x in place, and return None.\n",
      "    \n",
      "    Optional argument random is a 0-argument function returning a\n",
      "    random float in [0.0, 1.0); if it is the default None, the\n",
      "    standard random.random will be used.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(random.Random.shuffle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(398, 9)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function randint:\n",
      "\n",
      "randint(...) method of numpy.random.mtrand.RandomState instance\n",
      "    randint(low, high=None, size=None, dtype=int)\n",
      "    \n",
      "    Return random integers from `low` (inclusive) to `high` (exclusive).\n",
      "    \n",
      "    Return random integers from the \"discrete uniform\" distribution of\n",
      "    the specified dtype in the \"half-open\" interval [`low`, `high`). If\n",
      "    `high` is None (the default), then results are from [0, `low`).\n",
      "    \n",
      "    .. note::\n",
      "        New code should use the ``integers`` method of a ``default_rng()``\n",
      "        instance instead; see `random-quick-start`.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    low : int or array-like of ints\n",
      "        Lowest (signed) integers to be drawn from the distribution (unless\n",
      "        ``high=None``, in which case this parameter is one above the\n",
      "        *highest* such integer).\n",
      "    high : int or array-like of ints, optional\n",
      "        If provided, one above the largest (signed) integer to be drawn\n",
      "        from the distribution (see above for behavior if ``high=None``).\n",
      "        If array-like, must contain integer values\n",
      "    size : int or tuple of ints, optional\n",
      "        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then\n",
      "        ``m * n * k`` samples are drawn.  Default is None, in which case a\n",
      "        single value is returned.\n",
      "    dtype : dtype, optional\n",
      "        Desired dtype of the result. Byteorder must be native.\n",
      "        The default value is int.\n",
      "    \n",
      "        .. versionadded:: 1.11.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : int or ndarray of ints\n",
      "        `size`-shaped array of random integers from the appropriate\n",
      "        distribution, or a single such random int if `size` not provided.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random_integers : similar to `randint`, only for the closed\n",
      "        interval [`low`, `high`], and 1 is the lowest value if `high` is\n",
      "        omitted.\n",
      "    Generator.integers: which should be used for new code.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.randint(2, size=10)\n",
      "    array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0]) # random\n",
      "    >>> np.random.randint(1, size=10)\n",
      "    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n",
      "    \n",
      "    Generate a 2 x 4 array of ints between 0 and 4, inclusive:\n",
      "    \n",
      "    >>> np.random.randint(5, size=(2, 4))\n",
      "    array([[4, 0, 2, 1], # random\n",
      "           [3, 2, 2, 0]])\n",
      "    \n",
      "    Generate a 1 x 3 array with 3 different upper bounds\n",
      "    \n",
      "    >>> np.random.randint(1, [3, 5, 10])\n",
      "    array([2, 2, 9]) # random\n",
      "    \n",
      "    Generate a 1 by 3 array with 3 different lower bounds\n",
      "    \n",
      "    >>> np.random.randint([1, 5, 7], 10)\n",
      "    array([9, 8, 7]) # random\n",
      "    \n",
      "    Generate a 2 by 4 array using broadcasting with dtype of uint8\n",
      "    \n",
      "    >>> np.random.randint([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)\n",
      "    array([[ 8,  6,  9,  7], # random\n",
      "           [ 1, 16,  9, 12]], dtype=uint8)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "owner = np.random.randint(15000, 73000, (398,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function join in module pandas.core.frame:\n",
      "\n",
      "join(self, other, on=None, how='left', lsuffix='', rsuffix='', sort=False) -> 'DataFrame'\n",
      "    Join columns of another DataFrame.\n",
      "    \n",
      "    Join columns with `other` DataFrame either on index or on a key\n",
      "    column. Efficiently join multiple DataFrame objects by index at once by\n",
      "    passing a list.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    other : DataFrame, Series, or list of DataFrame\n",
      "        Index should be similar to one of the columns in this one. If a\n",
      "        Series is passed, its name attribute must be set, and that will be\n",
      "        used as the column name in the resulting joined DataFrame.\n",
      "    on : str, list of str, or array-like, optional\n",
      "        Column or index level name(s) in the caller to join on the index\n",
      "        in `other`, otherwise joins index-on-index. If multiple\n",
      "        values given, the `other` DataFrame must have a MultiIndex. Can\n",
      "        pass an array as the join key if it is not already contained in\n",
      "        the calling DataFrame. Like an Excel VLOOKUP operation.\n",
      "    how : {'left', 'right', 'outer', 'inner'}, default 'left'\n",
      "        How to handle the operation of the two objects.\n",
      "    \n",
      "        * left: use calling frame's index (or column if on is specified)\n",
      "        * right: use `other`'s index.\n",
      "        * outer: form union of calling frame's index (or column if on is\n",
      "          specified) with `other`'s index, and sort it.\n",
      "          lexicographically.\n",
      "        * inner: form intersection of calling frame's index (or column if\n",
      "          on is specified) with `other`'s index, preserving the order\n",
      "          of the calling's one.\n",
      "    lsuffix : str, default ''\n",
      "        Suffix to use from left frame's overlapping columns.\n",
      "    rsuffix : str, default ''\n",
      "        Suffix to use from right frame's overlapping columns.\n",
      "    sort : bool, default False\n",
      "        Order result DataFrame lexicographically by the join key. If False,\n",
      "        the order of the join key depends on the join type (how keyword).\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    DataFrame\n",
      "        A dataframe containing columns from both the caller and `other`.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    DataFrame.merge : For column(s)-on-columns(s) operations.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    Parameters `on`, `lsuffix`, and `rsuffix` are not supported when\n",
      "    passing a list of `DataFrame` objects.\n",
      "    \n",
      "    Support for specifying index levels as the `on` parameter was added\n",
      "    in version 0.23.0.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],\n",
      "    ...                    'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})\n",
      "    \n",
      "    >>> df\n",
      "      key   A\n",
      "    0  K0  A0\n",
      "    1  K1  A1\n",
      "    2  K2  A2\n",
      "    3  K3  A3\n",
      "    4  K4  A4\n",
      "    5  K5  A5\n",
      "    \n",
      "    >>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],\n",
      "    ...                       'B': ['B0', 'B1', 'B2']})\n",
      "    \n",
      "    >>> other\n",
      "      key   B\n",
      "    0  K0  B0\n",
      "    1  K1  B1\n",
      "    2  K2  B2\n",
      "    \n",
      "    Join DataFrames using their indexes.\n",
      "    \n",
      "    >>> df.join(other, lsuffix='_caller', rsuffix='_other')\n",
      "      key_caller   A key_other    B\n",
      "    0         K0  A0        K0   B0\n",
      "    1         K1  A1        K1   B1\n",
      "    2         K2  A2        K2   B2\n",
      "    3         K3  A3       NaN  NaN\n",
      "    4         K4  A4       NaN  NaN\n",
      "    5         K5  A5       NaN  NaN\n",
      "    \n",
      "    If we want to join using the key columns, we need to set key to be\n",
      "    the index in both `df` and `other`. The joined DataFrame will have\n",
      "    key as its index.\n",
      "    \n",
      "    >>> df.set_index('key').join(other.set_index('key'))\n",
      "          A    B\n",
      "    key\n",
      "    K0   A0   B0\n",
      "    K1   A1   B1\n",
      "    K2   A2   B2\n",
      "    K3   A3  NaN\n",
      "    K4   A4  NaN\n",
      "    K5   A5  NaN\n",
      "    \n",
      "    Another option to join using the key columns is to use the `on`\n",
      "    parameter. DataFrame.join always uses `other`'s index but we can use\n",
      "    any column in `df`. This method preserves the original DataFrame's\n",
      "    index in the result.\n",
      "    \n",
      "    >>> df.join(other.set_index('key'), on='key')\n",
      "      key   A    B\n",
      "    0  K0  A0   B0\n",
      "    1  K1  A1   B1\n",
      "    2  K2  A2   B2\n",
      "    3  K3  A3  NaN\n",
      "    4  K4  A4  NaN\n",
      "    5  K5  A5  NaN\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(pd.DataFrame.join)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8. Add the column owners to cars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "owner = pd.Series(owner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Other Series must have a name",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m--------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-94c4dcfc800d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcar\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mowner\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/miniconda3/lib/python3.8/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mjoin\u001b[0;34m(self, other, on, how, lsuffix, rsuffix, sort)\u001b[0m\n\u001b[1;32m   7865\u001b[0m         \u001b[0;36m5\u001b[0m  \u001b[0mK5\u001b[0m  \u001b[0mA5\u001b[0m  \u001b[0mNaN\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   7866\u001b[0m         \"\"\"\n\u001b[0;32m-> 7867\u001b[0;31m         return self._join_compat(\n\u001b[0m\u001b[1;32m   7868\u001b[0m             \u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mon\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mon\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlsuffix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlsuffix\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrsuffix\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrsuffix\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   7869\u001b[0m         )\n",
      "\u001b[0;32m~/miniconda3/lib/python3.8/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m_join_compat\u001b[0;34m(self, other, on, how, lsuffix, rsuffix, sort)\u001b[0m\n\u001b[1;32m   7877\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSeries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   7878\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 7879\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Other Series must have a name\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   7880\u001b[0m             \u001b[0mother\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   7881\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Other Series must have a name"
     ]
    }
   ],
   "source": [
    "car.join(other = owner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "car['owner'] = owner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model</th>\n",
       "      <th>origin</th>\n",
       "      <th>car</th>\n",
       "      <th>owner</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>307</td>\n",
       "      <td>130</td>\n",
       "      <td>3504</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet chevelle malibu</td>\n",
       "      <td>44092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.0</td>\n",
       "      <td>8</td>\n",
       "      <td>350</td>\n",
       "      <td>165</td>\n",
       "      <td>3693</td>\n",
       "      <td>11.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick skylark 320</td>\n",
       "      <td>23874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18.0</td>\n",
       "      <td>8</td>\n",
       "      <td>318</td>\n",
       "      <td>150</td>\n",
       "      <td>3436</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>plymouth satellite</td>\n",
       "      <td>44887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>304</td>\n",
       "      <td>150</td>\n",
       "      <td>3433</td>\n",
       "      <td>12.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>amc rebel sst</td>\n",
       "      <td>66753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>17.0</td>\n",
       "      <td>8</td>\n",
       "      <td>302</td>\n",
       "      <td>140</td>\n",
       "      <td>3449</td>\n",
       "      <td>10.5</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>ford torino</td>\n",
       "      <td>72007</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement horsepower  weight  acceleration  model  \\\n",
       "0  18.0          8           307        130    3504          12.0     70   \n",
       "1  15.0          8           350        165    3693          11.5     70   \n",
       "2  18.0          8           318        150    3436          11.0     70   \n",
       "3  16.0          8           304        150    3433          12.0     70   \n",
       "4  17.0          8           302        140    3449          10.5     70   \n",
       "\n",
       "   origin                        car  owner  \n",
       "0       1  chevrolet chevelle malibu  44092  \n",
       "1       1          buick skylark 320  23874  \n",
       "2       1         plymouth satellite  44887  \n",
       "3       1              amc rebel sst  66753  \n",
       "4       1                ford torino  72007  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car.head()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
